package org.stvd.common.freemarker.method;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;

/**
 * Freemarker自定义方法 URL编码
 */
@SuppressWarnings("deprecation")
public class URLEncoderMethod implements TemplateMethodModel {

    /**
     * 执行方法
     * 
     * @return Object 方法返回值
     * @throws TemplateModelException
     */
    @SuppressWarnings("rawtypes")
    public Object exec(List arg0) throws TemplateModelException {
        if (arg0.size() != 1) // 限定方法中必须且只能传递一个参数
        {
            throw new TemplateModelException("Wrong arguments!");
        }
        // 返回response.encodeURL执行结果
        String msg = "";
        try {
            msg = URLEncoder.encode((String) arg0.get(0), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // System.out.println(msg);
        return msg;
    }
}